import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { reqRecommendProductDetail } from "@/api/tourists";
interface ProductDetailType {
  productDetailData: any;
  loading: boolean;
}
const defaultState: ProductDetailType = {
  productDetailData: null,
  loading: true,
};
export const fetchProductDetailData = createAsyncThunk(
  "productDetail/fetchProductDetailData",
  async (id: string, thunkAPI) => {
    let { data } = await reqRecommendProductDetail(id);
    // if (res) {
    //   thunkAPI.dispatch(productDetailSlice.actions.fetchProductDetailData(res));
    // }
    return data;
  },
);
export const productDetailSlice = createSlice({
  name: "ProductDetail",
  initialState: defaultState,
  reducers: {
    fetchProductDetailData: (state, action: PayloadAction<any>) => {
      state.loading = false;
      state.productDetailData = action.payload;
    },
  },
  extraReducers: {
    [fetchProductDetailData.pending.type]: (state) => {},
    [fetchProductDetailData.fulfilled.type]: (state, action) => {
      state.loading = false;
      state.productDetailData = action.payload;
    },
    [fetchProductDetailData.rejected.type]: (state) => {},
  },
});
